package com.entor.hqj.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.alibaba.fastjson.JSON;
import com.entor.hqj.login.R;
import com.entor.hqj.util.HttpPostUtils;
import com.entor.hqj.util.MoreTextView;
import com.entor.hqj.util.StringUtil;
import com.entor.hqj.util.TitleBuilder;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.entor.hqj.model.voter;

public class FinishVoterActivity extends AppCompatActivity {

    private MoreTextView title = null;
    private MoreTextView content = null;
    private MoreTextView time = null;
    private MoreTextView type = null;
    private MoreTextView voter = null;
    private MoreTextView state = null;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_finish_voter);
        title = (MoreTextView) findViewById(R.id.et_title);
        content = (MoreTextView) findViewById(R.id.et_content);
        time = (MoreTextView) findViewById(R.id.et_time);
        type = (MoreTextView) findViewById(R.id.et_type);
        voter = (MoreTextView) findViewById(R.id.et_voter);
        state = (MoreTextView) findViewById(R.id.et_state);
        initTitle();
        getData();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public void initTitle() {
        new TitleBuilder(this).setLeftText("返回").setMidTitleText("已投票")
                .setLeftImgOrTextListener(new leftImgOrTextListener());
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("FinishVoter Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }

    class leftImgOrTextListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            finish();
        }
    }

    public void getData() {
        Intent intent = getIntent();
        Integer id = intent.getIntExtra("id", 0);
        Long vi_id = intent.getLongExtra("vi_id", 0);
        final String finish = "id=" + id + "&vi_id="+vi_id;
        new Thread() {
            @Override
            public void run() {
                String response =
                        HttpPostUtils.doPostRequest("voter/finishVoterByMobile", finish);
                Message message = Message.obtain();
                message.obj = response;
                String result = (String) message.obj;
                if (result != null) {
                    voter v = JSON.parseObject(result, voter.class);
                    title.setText(v.getTitle());
                    content.setText(v.getContent());
                    time.setText("完成投票用时:" + StringUtil.getTimeFormat(v.getCandidate(),v.getStarttime()));
                    type.setText("投票类型:一人一票");
                    voter.setText(v.getVoter());
                    state.setText("投票状态:按时完成投票");
                }
            }
        }.start();


    }


}
